package com.ljfiction.book.activitys;

import android.content.Intent;
import android.graphics.Color;
import android.icu.text.DecimalFormat;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.ljfiction.book.Baner.ChapterMoneyBean;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.SettingManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class ChapterMoneyActivity extends BaseActivity {

    @BindView(R.id.this_finishi)
    ImageView thisFinishi;
    @BindView(R.id.select_day)
    TextView selectDay;
    @BindView(R.id.left_text)
    TextView leftText;
    @BindView(R.id.reight_text)
    TextView reightText;
    @BindView(R.id.recycle_chaptermoney)
    RecyclerView recycleChaptermoney;
    @BindView(R.id.day_swich)
    TextView daySwich;
    @BindView(R.id.swich_chaptermoney)
    LinearLayout swichChaptermoney;
    private BaseQuickAdapter mAdapter;
    private boolean ispersion = true;
    private Intent intent;
    private Calendar calendar;
    private int start_year;
    private int start_month;
    private int start_day;
    private int end_year;
    private int end_month;
    private int end_day;
    private int page = 1;
    private int bookId;
    private DecimalFormat df;
    private ChapterMoneyBean chapterMoney;
    private List<ChapterMoneyBean.DataBean.ListBean> datas = new ArrayList<>();
    private Intent intentfrom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter_money_activity);
        ButterKnife.bind(this);
        intent = new Intent(this, MonthTimeActivity.class);
        calendar = Calendar.getInstance();
        bookId = getIntent().getIntExtra("bookId", 76);
        intentfrom = getIntent();
        inttime();
        intView();
        intData();
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/subscriptionstatistics")

                .params("bookid", String.valueOf(bookId))
                .params("startdate", start_year + "-" + isDan(start_month) + "-" + isDan(start_day) + " 00:00:00")
                .params("enddate", end_year + "-" + isDan(end_month) + "-" + isDan(end_day) + " 23:59:59")
                .params("startpage", String.valueOf(page))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson g = new Gson();
                        try {
                            chapterMoney = g.fromJson(s, ChapterMoneyBean.class);
                            datas.addAll(chapterMoney.getData().getList());
                            mAdapter.notifyDataSetChanged();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void intView() {
        recycleChaptermoney.setLayoutManager(new LinearLayoutManager(this));
        recycleChaptermoney.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        recycleChaptermoney.setAdapter(mAdapter = new BaseQuickAdapter<ChapterMoneyBean.DataBean.ListBean, BaseViewHolder>(R.layout.chaptermoney_item, datas) {
            @Override
            protected void convert(BaseViewHolder helper, ChapterMoneyBean.DataBean.ListBean item) {
                if (ispersion) {
                    helper.setText(R.id.num_chaptermoney, String.valueOf(item.getSumPersionCount()))
                            .setText(R.id.chaptername_chaptermoney, item.getBookChapterName());
                    helper.getView(R.id.xlj).setVisibility(View.GONE);

                } else {
                    helper.setText(R.id.num_chaptermoney, isAddDouhao(item.getSubscriptionPrice()))
                            .setText(R.id.chaptername_chaptermoney, item.getBookChapterName());
                    helper.getView(R.id.xlj).setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @OnClick({R.id.this_finishi, R.id.swich_chaptermoney,R.id.left_text,R.id.reight_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.this_finishi:
                goBack();
                break;
            case R.id.swich_chaptermoney:
                startActivityForResult(intent, 2);
                break;


            case R.id.left_text:
                leftText.setTextColor(Color.parseColor("#f77583"));
                leftText.setBackgroundResource(R.drawable.shape);
                reightText.setTextColor(Color.parseColor("#333333"));
                reightText.setBackgroundColor(Color.parseColor("#ffffff"));
                ispersion = true;
                mAdapter.notifyDataSetChanged();
                break;
            case R.id.reight_text:
                reightText.setTextColor(Color.parseColor("#f77583"));
                reightText.setBackgroundResource(R.drawable.shape);
                leftText.setTextColor(Color.parseColor("#333333"));
                leftText.setBackgroundColor(Color.parseColor("#ffffff"));
                ispersion = false;
                mAdapter.notifyDataSetChanged();
                break;
        }
    }

    private void inttime() {
        start_year = getIntent().getIntExtra("start_year", calendar.get(Calendar.YEAR));
        start_month = getIntent().getIntExtra("start_month", calendar.get(Calendar.MONTH) + 1);
        start_day = getIntent().getIntExtra("start_day", 1);
        end_year = getIntent().getIntExtra("end_year", calendar.get(Calendar.YEAR));
        end_month = getIntent().getIntExtra("end_month", calendar.get(Calendar.MONTH) + 1);
        end_day = getIntent().getIntExtra("end_day", calendar.get(Calendar.DAY_OF_MONTH));
        daySwich.setText(start_year + "."+start_month + "." + start_day + "-" +end_year+"."+ end_month + "." + end_day);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2 && resultCode == RESULT_OK) {
            start_year = data.getIntExtra("start_year", calendar.get(Calendar.YEAR));
            start_month = data.getIntExtra("start_month", calendar.get(Calendar.MONTH) + 1);
            start_day = data.getIntExtra("start_day", calendar.get(Calendar.DAY_OF_MONTH));

            end_year = data.getIntExtra("end_year", calendar.get(Calendar.YEAR));
            end_month = data.getIntExtra("end_month", calendar.get(Calendar.MONTH) + 1);
            end_day = data.getIntExtra("end_day", calendar.get(Calendar.DAY_OF_MONTH) + 1);
            daySwich.setText(start_year + "."+start_month + "." + start_day + "-" +end_year+"."+ end_month + "." + end_day);
            page = 1;
            datas.clear();
            intData();

        }
    }

    private String isDan(int dan) {
        if (dan < 10) {
            return "0" + String.valueOf(dan);
        } else {
            return String.valueOf(dan);
        }
    }

    private String isAddDouhao(int num) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            df = new DecimalFormat("#,###");
            return df.format(num);
        }
        return String.valueOf(num);

    }
}
